package com.sxfq.loansupermarket.backsystem.modular.remoteSynergy.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.common.persistence.model.Dept;
import org.apache.commons.lang.StringUtils;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.util.StringUtil;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.doctorLooked.entity.SfInfo;
import com.sxfq.loansupermarket.backsystem.modular.marketing.entity.TongJiInfo;
import com.sxfq.loansupermarket.backsystem.modular.remoteSynergy.dao.CaseSelectionDao;
import com.sxfq.loansupermarket.backsystem.modular.remoteSynergy.entity.*;
import com.sxfq.loansupermarket.backsystem.modular.remoteSynergy.service.ICaseSelectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class CaseSelectionServiceImpl implements ICaseSelectionService {
    @Autowired
    private CaseSelectionDao dao;

    @Autowired
    private SqlMapper sqlMapper;

    public List<CaseSelectionInfo> selectPageListByQueryGetMap(Page<CaseSelectionInfo> page, SearchCaseSelectionInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getReach())) {
            params.put("reach", searchInfo.getReach());
        }
        if (StringUtil.isNotEmpty(searchInfo.getIsSelection())) {
            if (searchInfo.getIsSelection() == 0) {
                if ("病例评选管理员".equals(ShiroKit.getUser().getRoleName())) {
                    params.put("judges", "");
                    params.put("isSelection", " and t.selection_status = " + searchInfo.getIsSelection() + " ");
                } else {
                    params.put("judges", " and t1.judges = '" + ShiroKit.getUser().getAccount() + "'");
                    params.put("isSelection", " and t1.selection_status = " + searchInfo.getIsSelection() + " ");
                }
                params.put("orderBy", " order by t.apply_time desc ");
            } else {
                if ("病例评选管理员".equals(ShiroKit.getUser().getRoleName())) {
                    params.put("judges", "");
                    params.put("isSelection", " and t.selection_status = " + searchInfo.getIsSelection() + " ");
                } else {
                    params.put("judges", " and t1.judges = '" + ShiroKit.getUser().getAccount() + "'");
                    params.put("isSelection", " and t1.selection_status = " + searchInfo.getIsSelection() + " ");
                }
                params.put("orderBy", " order by t.selection_time desc ");
            }
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartTime())) {
            if (searchInfo.getIsSelection() == 0) {
                params.put("startTime", " and t.apply_time >= '" + searchInfo.getStartTime() + "'");
            } else {
                params.put("startTime", " and t1.selection_time >= '" + searchInfo.getStartTime() + "'");
            }
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndTime())) {
            if (searchInfo.getIsSelection() == 0) {
                params.put("endTime", " and t.apply_time <= '" + searchInfo.getEndTime() + "'");
            } else {
                params.put("endTime", " and t1.selection_time <= '" + searchInfo.getEndTime() + "'");
            }
        }
        params.put("startIndex", searchInfo.getStartIndex());
        params.put("limitIndex", searchInfo.getLimitIndex());
        return dao.getQueryPages(page, params);
    }

    public int getQueryPagesCount(Page<CaseSelectionInfo> page, SearchCaseSelectionInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getReach())) {
            params.put("reach", searchInfo.getReach());
        }
        if (StringUtil.isNotEmpty(searchInfo.getIsSelection())) {
            if (searchInfo.getIsSelection() == 0) {
                if ("病例评选管理员".equals(ShiroKit.getUser().getRoleName())) {
                    params.put("judges", "");
                    params.put("isSelection", " and t.selection_status = " + searchInfo.getIsSelection() + " ");
                } else {
                    params.put("judges", " and t1.judges = '" + ShiroKit.getUser().getAccount() + "'");
                    params.put("isSelection", " and t1.selection_status = " + searchInfo.getIsSelection() + " ");
                }
                params.put("orderBy", " order by t.apply_time desc ");
            } else {
                if ("病例评选管理员".equals(ShiroKit.getUser().getRoleName())) {
                    params.put("judges", "");
                    params.put("isSelection", " and t.selection_status = " + searchInfo.getIsSelection() + " ");
                } else {
                    params.put("judges", " and t1.judges = '" + ShiroKit.getUser().getAccount() + "'");
                    params.put("isSelection", " and t1.selection_status = " + searchInfo.getIsSelection() + " ");
                }
                params.put("orderBy", " order by t.selection_time desc ");
            }
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartTime())) {
            if (searchInfo.getIsSelection() == 0) {
                params.put("startTime", " and t.apply_time >= '" + searchInfo.getStartTime() + "'");
            } else {
                params.put("startTime", " and t1.selection_time >= '" + searchInfo.getStartTime() + "'");
            }
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndTime())) {
            if (searchInfo.getIsSelection() == 0) {
                params.put("endTime", " and t.apply_time <= '" + searchInfo.getEndTime() + "'");
            } else {
                params.put("endTime", " and t1.selection_time <= '" + searchInfo.getEndTime() + "'");
            }
        }
        return dao.getQueryPagesCount(page, params);
    }

    public List<TongJiInfo> caseSelectiontotal(SearchCaseSelectionInfo searchInfo) {
        String contitionInfo = " ";
        String contitionInfo1 = " ";
        if (StringUtil.isNotEmpty(searchInfo.getIsSelection())) {
            if (searchInfo.getIsSelection() == 0) {
                if ("病例评选管理员".equals(ShiroKit.getUser().getRoleName())) {
                    contitionInfo += " and t.selection_status=0";
                    contitionInfo1 += " and t.selection_status=1";
                } else {
                    contitionInfo += " and t1.selection_status=0";
                    contitionInfo1 += " and t1.selection_status=1";
                    contitionInfo += " and t1.judges = '" + ShiroKit.getUser().getAccount() + "'";
                    contitionInfo1 += " and t1.judges = '" + ShiroKit.getUser().getAccount() + "'";
                }
            } else {
                if ("病例评选管理员".equals(ShiroKit.getUser().getRoleName())) {
                    contitionInfo += " and t.selection_status=0";
                    contitionInfo1 += " and t.selection_status=1";
                } else {
                    contitionInfo += " and t1.selection_status=0";
                    contitionInfo1 += " and t1.selection_status=1";
                    contitionInfo += " and t1.judges = '" + ShiroKit.getUser().getAccount() + "'";
                    contitionInfo1 += " and t1.judges = '" + ShiroKit.getUser().getAccount() + "'";
                }
            }
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartTime())) {
            if (searchInfo.getIsSelection() == 0) {
                contitionInfo += " and t.apply_time >= '" + searchInfo.getStartTime() + "' ";
            } else {
                contitionInfo1 += " and t1.selection_time >= '" + searchInfo.getStartTime() + "' ";
            }
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndTime())) {
            if (searchInfo.getIsSelection() == 0) {
                contitionInfo += " and t.apply_time <= '" + searchInfo.getEndTime() + "' ";
            } else {
                contitionInfo1 += " and t1.selection_time <= '" + searchInfo.getEndTime() + "' ";
            }
        }
        if (StringUtil.isNotEmpty(searchInfo.getReach())) {
            if (searchInfo.getIsSelection() == 0) {
                contitionInfo += " and (t.customer_name like CONCAT('%" + searchInfo.getReach() + "%') or t.szm like CONCAT('%" + searchInfo.getReach() + "%')) ";
            } else {
                contitionInfo1 += " and (t.customer_name like CONCAT('%" + searchInfo.getReach() + "%') or t.szm like CONCAT('%" + searchInfo.getReach() + "%')) ";
            }
        }
        String sql = "select count(*) as countNum from (select t.id from case_selection_information t left join case_selection_info t1 on t.id=t1.information_id " +
                " where t.save_status =1 " + contitionInfo + " group by t.id) t " +  //查询待提交病例
                " union all " +
                " select count(*) as countNum from (select t.id from case_selection_information t left join case_selection_info t1 on t.id=t1.information_id " +
                " where t.save_status =1 " + contitionInfo1 + " group by t.id) t";   //查询已提交病例
        return sqlMapper.selectList(sql, TongJiInfo.class);
    }

    public List<SfInfo> getMarkList() {
        String sql = "select * from custom_mark_info where user='" + ShiroKit.getUser().getAccount() + "'";
        List<SfInfo> list = sqlMapper.selectList(sql, SfInfo.class);
        return list;
    }

    public int insert(CaseSelectionUploadInfo info) {
        String deletesql = "delete from case_selection_score_info where information_id='" + info.getId() + "'";
        sqlMapper.delete(deletesql);
        String insertcon = "";
        String[] ratingItems = info.getRatingItems().split("&&", -1);
        String[] ratingItemsScore = info.getRatingItemsScore().split("&&", -1);
        for (int i = 0; i < ratingItems.length; i++) {
            if (i == ratingItems.length - 1) {
                insertcon += " (" + info.getId() + ",'" + ShiroKit.getUser().getAccount() + "','" + ratingItems[i] + "'" +
                        ",'" + ratingItemsScore[i] + "');";
            } else {
                insertcon += " (" + info.getId() + ",'" + ShiroKit.getUser().getAccount() + "','" + ratingItems[i] + "'" +
                        ",'" + ratingItemsScore[i] + "'),";
            }
        }
        String sql = "INSERT INTO `case_selection_score_info` (`information_id`, `judges`, `rating_items`, `score`) " +
                " VALUES " + insertcon + "";
        String sqlupdate = "update case_selection_info a set a.case_comment='" + info.getCaseComment() + "'" +
                ",a.mark='" + info.getMark() + "' where information_id='" + info.getId() + "' and judges='" + ShiroKit.getUser().getAccount() + "'";
        sqlMapper.update(sqlupdate);
        return sqlMapper.insert(sql);
    }

    public List<Dept> getOutpatientSelectionList() {
        String sql = "select id, simplename, fullname from dept where tips=5;";
        List<Dept> list = sqlMapper.selectList(sql, Dept.class);
        return list;
    }

    public List<CaseSelectionUploadInfo> getCaseOfOutpatientList(String obj) {
        String sql = "select * from case_selection_information a where a.apply_outpatient='" + obj + "' and a.save_status=1;";
        List<CaseSelectionUploadInfo> list = sqlMapper.selectList(sql, CaseSelectionUploadInfo.class);
        return list;
    }

    public CaseSelectionUploadInfo getCaseOfId(String obj) {
        String sql = "select * from case_selection_information where id=" + obj + " limit 1;";
        CaseSelectionUploadInfo caseSelectionUploadInfo = sqlMapper.selectOne(sql, CaseSelectionUploadInfo.class);
        String sql2 = "select * from case_selection_assistance_info where information_id=" + caseSelectionUploadInfo.getId() + ";";
        List<CaseSelectionAssistanceInfo> info2 = sqlMapper.selectList(sql2, CaseSelectionAssistanceInfo.class);
        caseSelectionUploadInfo.setCaseSelectionAssistanceInfoList(info2);
        return caseSelectionUploadInfo;
    }

    public CaseSelectionUploadInfo getCaseOfIdOrName(String obj) {
        String sql = "select (select count(s.votes)+1 from case_selection_information s where s.votes>t.votes) as voteRanking," +
                "ifnull(((select s.votes from case_selection_information s where s.votes>t.votes limit 1)-votes),0) as lastDisparityVotes," +
                "(select b.avatar from user b where b.account=t.creater) as titleImg,t.* from case_selection_information t where id=" + obj + " or applyer='" + obj + "' limit 1;";
        CaseSelectionUploadInfo caseSelectionUploadInfo = sqlMapper.selectOne(sql, CaseSelectionUploadInfo.class);
        String sql2 = "select * from case_selection_assistance_info where information_id=" + caseSelectionUploadInfo.getId() + ";";
        List<CaseSelectionAssistanceInfo> info2 = sqlMapper.selectList(sql2, CaseSelectionAssistanceInfo.class);
        caseSelectionUploadInfo.setCaseSelectionAssistanceInfoList(info2);
        return caseSelectionUploadInfo;
    }

    public int update(CaseSelectionUploadInfo info) {
        String deletesql = "delete from case_selection_score_info where information_id='" + info.getId() + "'";
        sqlMapper.delete(deletesql);
        String insertcon = "";
        Double score = 0.00;
        String[] ratingItems = info.getRatingItems().split("&&", -1);
        String[] ratingItemsScore = info.getRatingItemsScore().split("&&", -1);
        for (int i = 0; i < ratingItems.length; i++) {
            if (i == ratingItems.length - 1) {
                insertcon += " (" + info.getId() + ",'" + ShiroKit.getUser().getAccount() + "','" + ratingItems[i] + "'" +
                        ",'" + ratingItemsScore[i] + "');";
            } else {
                insertcon += " (" + info.getId() + ",'" + ShiroKit.getUser().getAccount() + "','" + ratingItems[i] + "'" +
                        ",'" + ratingItemsScore[i] + "'),";
            }
            score += Double.parseDouble(ratingItemsScore[i]);
        }
        String sql = "INSERT INTO `case_selection_score_info` (`information_id`, `judges`, `rating_items`, `score`) " +
                " VALUES " + insertcon + "";
        String sqlupdate = "update case_selection_info a set a.case_comment='" + info.getCaseComment() + "'" +
                ",a.mark='" + info.getMark() + "',a.selection_score=" + score + ",selection_status=1 ,a.selection_time='" + info.getSelectionTime() + "'" +
                " where information_id='" + info.getId() + "' and judges='" + ShiroKit.getUser().getAccount() + "'";
        sqlMapper.update(sqlupdate);
        return sqlMapper.insert(sql);
    }

    public List<CaseSelectionUploadInfo> getCaseByIdOrName(String obj) {
        String sql = "";
        if (StringUtils.isNotEmpty(obj)) { // 如果通过id查询
            sql = "select u.avatar as avatar,u.`name` as doctorName,a.* from case_selection_information a left join user u on a.applyer = u.account " +
                    "where 1=1 and a.save_status=1 and (a.id = '" + obj + "' or u.name= '" + obj + "') limit 1;";
        }
        List<CaseSelectionUploadInfo> list = sqlMapper.selectList(sql, CaseSelectionUploadInfo.class);
        return list;
    }
}